package com.lmscn.lms.api.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.lmscn.lms.api.model.*;
import com.lmscn.lms.api.util.JsonKit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by quxingyu on 2015/8/26.
 */
public class QuestionController extends Controller {
	
	public static final String COURSE_TEST_INSIDE = "course_test_inside";

	/**
	 * 题目添加收藏
	 * 
	 * @modified 2016年12月21日13:56:27
	 */
	public void savaQuestionFavorite() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			Long studentId = getParaToLong("studentId");
			Long testpaperId = getParaToLong("testpaperId");
			Long questionId = getParaToLong("questionId");
			String type = getPara("type");

			TestpaperResult testpaperResult = TestpaperResult.dao.getByStuIdAndTestpaperId(studentId, testpaperId);
			Question question = Question.dao.findById(questionId);
			QuestionCategory questionCategory = QuestionCategory.dao.findById(question.get("category_id"));

			QuestionFavorite q = QuestionFavorite.dao.getFavorite(studentId, questionId);
			if (q == null) {
				QuestionFavorite questionFavorite = getModel(QuestionFavorite.class);
				QuestionFavorite.dao.saveQuestionFavorite(questionFavorite, question, questionCategory, testpaperResult, type);

				dataMap.put("error_code", "0");
				dataMap.put("msg", "题目收藏成功！");
				dataMap.put("data", data);
			} else {
				dataMap.put("error_code", "0");
				dataMap.put("msg", "已经收藏过该题目！");
				dataMap.put("data", data);
			}

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "题目收藏失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));

	}
	
	/**
	 * 收藏课程练习题目接口
	 * 
	 * @date 2017年3月8日14:10:01
	 * @comp 2017年3月8日14:16:34
	 */
	public void savaCourseTestQuestionFavorite() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		
		try {
			
			Long studentId = getParaToLong("studentId");
			Long courseId = getParaToLong("courseId");
			Long questionId = getParaToLong("questionId");
			
			// 固定字符串
			String type = COURSE_TEST_INSIDE;
			
			Question question = Question.dao.findById(questionId);
			QuestionCategory questionCategory = QuestionCategory.dao.findById(question.get("category_id"));
			
			QuestionFavorite q = QuestionFavorite.dao.getFavorite(studentId, questionId);
			if (q == null) {
				QuestionFavorite questionFavorite = getModel(QuestionFavorite.class);
				QuestionFavorite.dao.saveCourseTestQuestionFavorite(questionFavorite, question, questionCategory, studentId,courseId, type);
				
				dataMap.put("error_code", "0");
				dataMap.put("msg", "题目收藏成功！");
				dataMap.put("data", data);
			} else {
				dataMap.put("error_code", "0");
				dataMap.put("msg", "已经收藏过该题目！");
				dataMap.put("data", data);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			
			dataMap.put("error_code", "1");
			dataMap.put("msg", "题目收藏失败！");
			dataMap.put("data", data);
		}
		
		renderJson(JsonKit.mapToJson(dataMap, 100));
		
	}

	/**
	 * 取消题目收藏接口
	 * 
	 * @date 2016年12月22日16:29:32
	 */
	public void cancleQuestionFavarite() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		Long studentId = getParaToLong("studentId");
		Long questionId = getParaToLong("questionId");
		try {
			QuestionFavorite.dao.deleteMyCollection(questionId, studentId);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "取消题目收藏成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "取消题目收藏失败！");
			dataMap.put("data", data);
		}
		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 
	 * 删除收藏
	 */
	public void deleteQuestionFavarite() {
		String ids = getPara("ids");
		String[] str = ids.split(",");
		for (int i = 0; i < str.length; i++) {
			Long id = Long.valueOf(str[i]);
			QuestionFavorite.dao.deleteQuestionFavorite(id);
		}
	}

	/**
	 * 获取收藏题目列表，根据收藏时间倒序
	 */
	public void getQuestionFavarite() {
		int pageNumber = getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize");
		String key = getPara("key");
		String type = getPara("type");
		String targetType = getPara("targetType");
		Page<QuestionFavorite> page = QuestionFavorite.dao.getQuestionFavorites(pageNumber, pageSize, type, key, targetType);
		List<QuestionFavorite> list = page.getList();
		renderJson(JsonKit.listToJson(list, 39));
	}

}
